package com.csdj.service.impl;

import com.csdj.service.excelService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: DengYuChang
 * @date: 2023-09-06 9:07
 * @description:
 */
@Service
public class excelServiceImpl implements excelService {
    @Override
    public void exportDataToEx(HttpServletResponse response, List<List<String>> list) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("销账明细报表");

        // 设置要导出的文件的名字
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");



        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"交费单号","发票号","发票类型","交费日期","交费金额",
                "已用金额","剩余金额","收费员","生活用水量","商业用水量","工业用水量","行政用水量","军区用水量","特种用水量",
                "销账总水量","生活用水销账","商业用水销账","工业用水销账","行政用水销账",
                "军区用水销账","特种用水销账","垃圾费销账","污水费销账","资源费销账","销账总金额"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (List<String> l  : list) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(l.get(0));
            row1.createCell(1).setCellValue(l.get(1));
            row1.createCell(2).setCellValue(l.get(2));
            row1.createCell(3).setCellValue(l.get(3));
            row1.createCell(4).setCellValue(l.get(4));
            row1.createCell(5).setCellValue(l.get(5));
            row1.createCell(6).setCellValue(l.get(6));
            row1.createCell(7).setCellValue(l.get(7));
            row1.createCell(8).setCellValue(l.get(8));
            row1.createCell(9).setCellValue(l.get(9));
            row1.createCell(10).setCellValue(l.get(10));
            row1.createCell(11).setCellValue(l.get(11));
            row1.createCell(12).setCellValue(l.get(12));
            row1.createCell(13).setCellValue(l.get(13));
            row1.createCell(14).setCellValue(l.get(14));
            row1.createCell(15).setCellValue(l.get(15));
            row1.createCell(16).setCellValue(l.get(16));
            row1.createCell(17).setCellValue(l.get(17));
            row1.createCell(18).setCellValue(l.get(18));
            row1.createCell(19).setCellValue(l.get(19));
            row1.createCell(20).setCellValue(l.get(20));
            row1.createCell(21).setCellValue(l.get(21));
            row1.createCell(22).setCellValue(l.get(22));
            row1.createCell(23).setCellValue(l.get(23));
            row1.createCell(24).setCellValue(l.get(24));
            rowNum++;
        }

        String codedFileName = java.net.URLEncoder.encode("销账明细报表"+sf.format(new Date()), "UTF-8");
        response.setContentType("application/octet-stream");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + codedFileName+ ".xls");
        response.flushBuffer();
        OutputStream outputStream = response.getOutputStream();
        workbook.write(response.getOutputStream());
        outputStream.flush();
        outputStream.close();
    }
}
